(function() {
  var iced, jobs, moment, pgsql, sched, send_notify, take_merch_effect, uuid, __iced_k, __iced_k_noop;

  iced = require('iced-runtime');
  __iced_k = __iced_k_noop = function() {};

  sched = require('node-schedule');

  pgsql = require('pg');

  moment = require('moment');

  uuid = require('uuid');

  jobs = {};

  module.exports = jobs;

  send_notify = function(message, detail, level) {
    var client, done, err, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/home/qiwei/Desktop/luckpay-web/coffee/lib/merch_jobs.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 14
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          return done(client) && console.log(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/home/qiwei/Desktop/luckpay-web/coffee/lib/merch_jobs.coffee"
          });
          client.query("insert into web_notify ( sender, message, detail, level ) values ( $1, $2, $3, $4 )", [1, message, detail, level], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 23
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            return done(client) && console.log(err);
          }
          return done();
        });
      };
    })(this));
  };

  take_merch_effect = function(merch_uuid) {
    var client, done, err, record, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/home/qiwei/Desktop/luckpay-web/coffee/lib/merch_jobs.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 30
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          return done(client) && console.log(err);
        }
        client.query("begin");
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/home/qiwei/Desktop/luckpay-web/coffee/lib/merch_jobs.coffee"
          });
          client.query("select * from pay_merch_ex where uuid = $1 for update", [merch_uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 37
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            return done(client) && console.log(err);
          }
          if (result.rows.length === 0) {
            client.query("rollback;");
            if (err) {
              return done(client) && console.log(err);
            }
            done();
            return console.log('未找到待生效的商户记录');
          }
          record = result.rows[0];
          if (record.rejected) {
            client.query("rollback");
            done();
            return console.log('商户信息复核时被拒绝，不能生效');
          }
          if (!record.checked) {
            client.query("rollback");
            done();
            return console.log('商户信息尚未复核，不能生效');
          }
          switch (record.action) {
            case 1:
            case 2:
              (function(__iced_k) {
                __iced_deferrals = new iced.Deferrals(__iced_k, {
                  parent: ___iced_passed_deferral,
                  filename: "/home/qiwei/Desktop/luckpay-web/coffee/lib/merch_jobs.coffee"
                });
                client.query("update pay_merch set inside = $1,             chnl = $2,             merno = $3, trctl = $4,              settl_merid = $5,      mapto_merid = $6, name = $7,               contact = $8,          telephone = $9, mobile = $10,            fax = $11,             email = $12, address = $13, unit_name = $14,         unit_name_abbr = $15,  unit_english = $16, unit_english_abbr = $17, unit_code = $18,       unit_license = $19, unit_license_exp = $20,  unit_tax_code = $21,   unit_issuer = $22, unit_address = $23,      unit_type = $24,       unit_legal_name = $25, unit_cred_type = $26,    unit_cred_no = $27,    unit_assets = $28, unit_scope = $29, keys = $30,              notes = $31, inputer = $32,           input_time = $33, checker = $34,           check_time = $35, effect_time = $36,       shaded = false,        t20_fee = $37 where uuid = $38", [record.inside, record.chnl, record.merno, record.trctl, record.settl_merid, record.mapto_merid, record.name, record.contact, record.telephone, record.mobile, record.fax, record.email, record.address, record.unit_name, record.unit_name_abbr, record.unit_english, record.unit_english_abbr, record.unit_code, record.unit_license, record.unit_license_exp, record.unit_tax_code, record.unit_issuer, record.unit_address, record.unit_type, record.unit_legal_name, record.unit_cred_type, record.unit_cred_no, record.unit_assets, record.unit_scope, record.keys, record.notes, record.inputer, record.input_time, record.checker, record.check_time, record.effect_time, record.t20_fee, record.ref_uuid], __iced_deferrals.defer({
                  assign_fn: (function() {
                    return function() {
                      err = arguments[0];
                      return result = arguments[1];
                    };
                  })(),
                  lineno: 92
                }));
                __iced_deferrals._fulfill();
              })(function() {
                (function(__iced_k) {
                  if (err) {
                    console.error(err.message);
                    if (err.detail) {
                      console.error(err.detail);
                    }
                    client.query("rollback");
                    done();
                    return sched.scheduleJob(moment().add('1', 'm').toDate(), function() {
                      return take_merch_effect(merch_uuid);
                    });
                    return __iced_k();
                  } else {
                    (function(__iced_k) {
                      __iced_deferrals = new iced.Deferrals(__iced_k, {
                        parent: ___iced_passed_deferral,
                        filename: "/home/qiwei/Desktop/luckpay-web/coffee/lib/merch_jobs.coffee"
                      });
                      client.query("delete from pay_merch_ex where uuid = $1", [merch_uuid], __iced_deferrals.defer({
                        assign_fn: (function() {
                          return function() {
                            err = arguments[0];
                            return result = arguments[1];
                          };
                        })(),
                        lineno: 105
                      }));
                      __iced_deferrals._fulfill();
                    })(function() {
                      client.query("commit");
                      return done();
                      return __iced_k();
                    });
                  }
                })(__iced_k);
              });
              break;
            case 3:
              (function(__iced_k) {
                __iced_deferrals = new iced.Deferrals(__iced_k, {
                  parent: ___iced_passed_deferral,
                  filename: "/home/qiwei/Desktop/luckpay-web/coffee/lib/merch_jobs.coffee"
                });
                client.query("delete from pay_merch_ex where uuid = $1", [merch_uuid], __iced_deferrals.defer({
                  assign_fn: (function() {
                    return function() {
                      err = arguments[0];
                      return result = arguments[1];
                    };
                  })(),
                  lineno: 112
                }));
                __iced_deferrals._fulfill();
              })(function() {
                (function(__iced_k) {
                  __iced_deferrals = new iced.Deferrals(__iced_k, {
                    parent: ___iced_passed_deferral,
                    filename: "/home/qiwei/Desktop/luckpay-web/coffee/lib/merch_jobs.coffee"
                  });
                  client.query("delete from pay_merch where uuid = $1", [record.ref_uuid], __iced_deferrals.defer({
                    assign_fn: (function() {
                      return function() {
                        err = arguments[0];
                        return result = arguments[1];
                      };
                    })(),
                    lineno: 118
                  }));
                  __iced_deferrals._fulfill();
                })(function() {
                  if (err) {
                    console.error(err.message);
                    if (err.detail) {
                      console.error(err.detail);
                    }
                    client.query("rollback");
                    done();
                    sched.scheduleJob(moment().add('1', 'm').toDate(), function() {
                      return take_merch_effect(merch_uuid);
                    });
                  } else {
                    client.query("commit");
                    return done();
                  }
                  return __iced_k();
                });
              });
              break;
            default:
              return __iced_k(send_notify('系统错误', "复核表中 action 无效，UUID = " + merch_uuid, 4));
          }
        });
      };
    })(this));
  };

  jobs.sched_effect = function(merch_uuid, effect_time) {
    if (typeof effect_time === 'string') {
      effect_time = moment(effect_time);
    }
    if (effect_time.isBefore(moment().add('1', 'm'))) {
      return take_merch_effect(merch_uuid);
    } else {
      return sched.scheduleJob(effect_time.toDate(), function() {
        return take_merch_effect(merch_uuid);
      });
    }
  };

  jobs.add_all = function() {
    var client, done, err, record, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/home/qiwei/Desktop/luckpay-web/coffee/lib/merch_jobs.coffee",
          funcname: "add_all"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 151
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          return done(client) && next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/home/qiwei/Desktop/luckpay-web/coffee/lib/merch_jobs.coffee",
            funcname: "add_all"
          });
          client.query("select * from pay_merch_ex where checked = true", __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 156
          }));
          __iced_deferrals._fulfill();
        })(function() {
          var _i, _len, _ref, _results;
          if (err) {
            return done(client) && next(err);
          }
          done();
          _ref = result.rows;
          _results = [];
          for (_i = 0, _len = _ref.length; _i < _len; _i++) {
            record = _ref[_i];
            _results.push(jobs.sched_effect(record.uuid, record.effect_time));
          }
          return _results;
        });
      };
    })(this));
  };

}).call(this);
